package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.service.interfaces.IMessage;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport {
	
	private static final long serialVersionUID = -249396030692382302L;

//	ApplicationContext ac;
	
	private IMessage messageServ;
	
	private List<Message> messageList;
	private Message message = new Message();
	private Page page = new Page();

	public IMessage getMessageServ() {
		return messageServ;
	}

	public void setMessageServ(IMessage messageServ) {
		this.messageServ = messageServ;
	}

	public List<Message> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
//	public MessageAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		messageServ = (IMessage) ac.getBean("IMessage");
//	}

	public String getMessageListForPage() {
		int count = messageServ.getCount(message);
		page.setCount(count);
		messageList = messageServ.getMessageList(message, page);
		return "messageList.jsp";
	}

	public void searchMessageList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = messageServ.getCount(message);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Message> list = messageServ.getMessageList(message, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Message s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getContent())) {
					map.put("content", s.getContent());
				} else {
					map.put("content", "");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (!StringUtils.isBlank(s.getPhone())) {
					map.put("phone", s.getPhone());
				} else {
					map.put("phone", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 保存留言信息
	 */
	public void saveNotice() {
//		Json json = new Json();
//		boolean result = messageServ.saveMessage(message);
//		if (result) {
//			json.setMessage("添加成功");
//		} else {
//			json.setSuccess(false);
//			json.setMessage("添加失败");
//		}
//		JsonUtil.writeJson(json);
	}

	public String toEditNotice() {
//		message = messageServ.getMessageById(message.getTimeKey());
//		return "updateNotice.jsp";
		return "";
	}

	public void updateNotice() {
//		Json json = new Json();
//		Notice tmp = noticeServ.getNoticeById(notice.getTimeKey());
//		if (tmp != null) {
//			tmp.setTitle(notice.getTitle());
//			tmp.setContent(notice.getContent());
//			tmp.setCreateDate(new Date());
//			boolean result = noticeServ.updateNotice(tmp);
//			if (result) {
//				json.setMessage("编辑成功");
//				notice = tmp;
//			} else {
//				json.setSuccess(true);
//				json.setMessage("编辑失败");
//			}
//		} else {
//			json.setSuccess(true);
//			json.setMessage("编辑失败");
//		}
//		JsonUtil.writeJson(json);
	}

	public void delNotice() {
//		Json json = new Json();
//		boolean result = noticeServ.delNotice(notice);
//		if (result) {
//			json.setMessage("删除成功");
//		} else {
//			json.setSuccess(false);
//			json.setSuccess(false);
//		}
//		JsonUtil.writeJson(json);
	}
	
	// 显示留言详细信息
	public String showMessage() {
		message = messageServ.getMessageById(message.getTimeKey());
		return "showMessage.jsp";
	}

}